package com.gamerole.prepare.schedule;


import me.xuxiaoxiao.chatapi.wechat.WeChatClient;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Scheduler {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    WeChatClient weChatClient;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/1 * * * ?") //每分钟执行一次
    public void statusCheck() {
        logger.info("每分钟执行一次。开始……");
        if (weChatClient==null){
            return;
        }
        WXUser wxUser = weChatClient.userFriend(redisTemplate.opsForValue().get("陌陌"));
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sf.format(new Date());
        weChatClient.sendText(wxUser, time);
        weChatClient.sendText(wxUser, "上午8:00 吕志豪");
        logger.info("每分钟执行一次。结束。");
    }

//    @Scheduled(fixedRate = 20000)
//    public void testTasks() {
//        logger.info("每20秒执行一次。开始……");
//        //statusTask.healthCheck();
//        logger.info("每20秒执行一次。结束。");
//    }
}
